import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//删除操作
public class JdbcDemo3 {
    public static void main(String[] args) throws SQLException {
        //1.创造数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");//设置url
        ((MysqlDataSource)dataSource).setUser("root");//设置用户名
        ((MysqlDataSource)dataSource).setPassword("2003");//设置密码

        //2.和数据库建立连接
        Connection connection = dataSource.getConnection();

        //3.设立sql语句和语句对象
        String sql = "delete from student where id = 7";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //4.执行语句
        int n = preparedStatement.executeUpdate();
        System.out.println("n="+n);

        //5.关闭资源
        preparedStatement.close();
        connection.close();
    }
}
